//存放旅客集合
var travellerList = new Array();

layui.use('laydate', function () {
    var laydate = layui.laydate;
    //自定义颜色
    laydate.render({
        elem: '#test30'
        , theme: '#00bcd4'
    });
});


$(function () {
    $(window).scroll(function () {
        //获取垂直滚动的距离
        var scrollTop = $(document).scrollTop();
        if (scrollTop >= 300) {
            $("#js_p_sidebar").css({"top": "0", "position": "fixed"});
        } else {
            $("#js_p_sidebar").css({"top": "200px", "position": "absolute"});
        }
    });
});
/*====================渲染确认订单基本信息======================*/
var url = window.location.href;//获得url
var index = url.indexOf("?");//？在url的下标

url = url.substr(index + 1);//获得参数字符串
console.log("url:" + url);

if (url.indexOf("proId") != -1) {
    var proId = url.split("&")[0].split("=")[1];
    console.log("productId:" + proId)
    proId = decodeURIComponent(proId);
}
//获得出发日期
if (url.indexOf("goDate") != -1) {
    var goDate = url.split("&")[1].split("=")[1];

    goDate = decodeURIComponent(goDate);
    $(".input_date").val(goDate);
}
//获得成人数量
if (url.indexOf("adultNum") != -1) {
    var adultNum = url.split("&")[2].split("=")[1];

    adultNum = decodeURIComponent(adultNum);
    $(".js-adult-num").val(adultNum);
}
//获得儿童数量
if (url.indexOf("childNum") != -1) {
    var childNum = url.split("&")[3].split("=")[1];

    childNum = decodeURIComponent(childNum);
    $(".js-child-num").val(childNum);
}
//获得成人价格&儿童价格(成人价格半价)

if (url.indexOf("adultPrice") != -1) {

    var adultPrice = url.split("&")[4].split("=")[1];
    adultPrice = decodeURIComponent(adultPrice);
    var childPrice = adultPrice / 2;
    $(".per-adult-price").html(adultPrice);
    $(".per-child-price").html(childPrice);
}
if (url.indexOf("rushTime") != -1) {
    console.log(url.indexOf("rushTime"));
    var rushTime = url.split("&")[7].split("=")[1];
    $(".rushTime").val(rushTime);
}
if (url.indexOf("secKillId") != -1) {
    var secKillId = url.split("&")[8].split("=")[1];
    $(".secKillId").val(secKillId);
}


if (url.indexOf("satrtCity") != -1 && url.indexOf("proName") != -1) {
    var xianlu = $(".black");
    xianlu.html("");
    var proName = url.split("&")[5].split("=")[1];
    var satrtCity = url.split("&")[6].split("=")[1];
    proName = decodeURIComponent(proName);
    satrtCity = decodeURIComponent(satrtCity);
    var protitle = "  <span class=\"js_pre_city\">[" + satrtCity + "出发]</span>\n" +
        "         <input type='hidden'class='proID' value='" + proId + "'>                       <a href=\"/product/getOne/" + proId + "\" class=\"blue\" target=\"_blank\">" + proName + "</a>"
    xianlu.append(protitle);

}
updatePrice();
updateTraveller();

/*===========加减游客数量====================*/
function addadultNum() {
    var adultNum = $(".js-adult-num").val();
    adultNum++;
    $(".js-adult-num").val(adultNum);
    updatePrice();
    updateTraveller();

}

function addchildNum() {
    var childNum = $(".js-child-num").val();
    childNum++;
    $(".js-child-num").val(childNum);
    updatePrice();
    updateTraveller();
}

function subadultNum() {
    var adultNum = $(".js-adult-num").val();

    if (adultNum == 1) {
        return false;
    } else {
        adultNum--;
        $(".js-adult-num").val(adultNum);
        updatePrice();
        updateTraveller();
    }
}

function subchildNum() {
    var childNum = $(".js-child-num").val();
    if (childNum == 0) {
        return false;
    } else {
        childNum--;
        $(".js-child-num").val(childNum);
        updatePrice();
        updateTraveller();
    }
}

/*===========更新渲染价格=================*/
function updatePrice() {
    var adultPrice = $(".per-adult-price").html() / 1;
    var childPrice = adultPrice / 2;
    var childNum = $(".js-child-num").val();
    var adultNum = $(".js-adult-num").val();
    var sumPrice = adultPrice * adultNum + childNum * childPrice;//总价
    $(".order-price").html(sumPrice);
    $(".order-adult-num").html(adultNum);
    $(".order-adult-price").html(adultPrice);

    $(".order-child-num").html(childNum);
    $(".order-child-price").html(childPrice);
    $(".order-price").html(sumPrice);
    $(".order-price2").html("￥" + sumPrice);
}


function updateTraveller() {
    var adultNum = $(".js-adult-num").val();
    var adultlist = $(".adult-list");
    var childNum = $(".js-child-num").val();
    var childlist = $(".child-list");
    adultlist.html("");
    childlist.html("");
    if (adultNum != 0) {
        for (var i = 1; i <= adultNum; i++) {
            var adultli = "<li class=\"adult_passenger-li\">\n" +
                "                                        <strong>旅客<span class=\"num\">" + i + "</span> </strong>\n" +
                "                                        <span class=\"passenger-title\">成人(年龄>12岁)</span>\n" +
                "                                        <ul  class=\"tra-list adulttripper_list\">\n" +
                "                                            <li>\n" +
                "                                                <span class=\"label\">姓名</span><input type=\"text\" name=\"user-name\"\n" +
                "                                                                onblur='checkName(this)'         class=\"user-name\">\n" +
                "                                                <span class=\"error-msg\" >错误信息</span>\n" +
                "                                            </li>\n" +
                "                                            <li>\n" +
                "                                                <span class=\"label\">身份证号码</span><input type=\"text\" name=\"user-idno\"\n" +
                "                                                              onblur='checkIdNo(this)'          class=\"user-idno\">\n" +
                "                                                <span class=\"error-msg\">错误信息</span>\n" +
                "                                            </li>\n" +
                "                                            <li class=\"li-telno\">\n" +
                "                                                <span class=\"label\">手机号码</span><input type=\"text\" name=\"user-telno\"\n" +
                "                                                           onblur='checktelNo(this)'             class=\"user-telno\">\n" +
                "                                                <span class=\"error-msg\">错误信息</span>\n" +
                "                                            </li>\n" +
                "                                        </ul>\n" +
                "                                    </li>"
            adultlist.append(adultli);
        }
        if (childNum != 0) {
            for (var i = 1; i <= childNum; i++) {
                var num = i / 1 + adultNum / 1;

                var childli = " <li class=\"adult_passenger-li\">\n" +
                    "                                        <strong>旅客<span class=\"num\">" + num + "</span></strong>\n" +
                    "                                        <span class=\"passenger-title\">儿童(年龄<=12岁)</span>\n" +
                    "                                        <ul  class=\"tra-list childtripper_list\">\n" +
                    "                                            <li>\n" +
                    "                                                <span class=\"label\">姓名</span><input type=\"text\" name=\"user-name\"\n" +
                    "                                                        onblur='checkName(this)'        class=\"user-name\">\n" +
                    "                                                <span class=\"error-msg\"  >错误信息</span>\n" +
                    "                                            </li>\n" +
                    "                                            <li>\n" +
                    "                                                <span class=\"label\">身份证号码</span><input type=\"text\" name=\"user-idno\"\n" +
                    "                                                    onblur='checkIdNo(this)'    class=\"user-idno\">\n" +
                    "                                                <span class=\"error-msg\">错误信息</span>\n" +
                    "                                            </li>\n" +
                    "                                            <li class=\"li-telno\">\n" +
                    "                                                <span class=\"label\">手机号码</span><input type=\"text\" name=\"user-telno\"\n" +
                    "                                                                  onblur='checktelNo(this)'          class=\"user-telno\">\n" +
                    "                                                <span class=\"error-msg\">错误信息</span>\n" +
                    "                                            </li> <input type='hidden' class='user-tripper'>\n" +
                    "                                        </ul>\n" +
                    "                                    </li>"
                childlist.append(childli);
            }
        }
    }
}

function getTripper() {
    travellerList.splice(0,travellerList.length-1);
    var tripperlist = $(".adulttripper_list");
    for (var i = 0; i < tripperlist.length; i++) {
        var messageList = $(tripperlist[i]).find("input");
        var nameEle = messageList[0];
        var idNoEle = messageList[1];
        var telNoEle = messageList[3];
        var travellerName = $(nameEle).val();
        var certificateNo = $(idNoEle).val();
        var travellerTelno = $(telNoEle).val();
        var travellerType = "成人";
        travellerList[i]={
            id: "",
            customerId: "",
            travellerName: travellerName,
            certificateType: "身份证",
            certificateNo: certificateNo,
            travellerTelno: travellerTelno,
            travellerGender: "",
            travellerType: travellerType,
        };

    }

}

//检查用户身份证格式
function checkIdNo(obj) {
    var idNo = $(obj).val();
    var errorMsg = $(obj).next();
    errorMsg.html("")

    if (idNo == "" || idNo == null) {
        errorMsg.html("身份证号码不能为空");
        errorMsg.show();
        return false;
    } else if (/^\d{15}|\d{18}$/.test(idNo) == false) {
        errorMsg.html("请输入正确的格式");
        errorMsg.show();
        return false;
    } else {
        errorMsg.hide();
        return true;
    }
}

//验证姓名
function checkName(obj) {
    var userName = $(obj).val();
    var errorMsg = $(obj).next();
    errorMsg.html("")

    if (userName == "" || userName == null) {
        errorMsg.html("姓名不能为空");
        errorMsg.show();
        return false;
    } else if (/^[\u4e00-\u9fa5]{0,}$/.test(userName) == false) {
        errorMsg.html("请输入正确的格式");
        errorMsg.show();
        return false;
    } else {
        errorMsg.hide();
        return true;
    }
}

//验证用户电话号码
function checktelNo(obj) {
    var telNo = $(obj).val();
    var errorMsg = $(obj).next();
    errorMsg.html("")
    if (telNo == "" || telNo == null) {
        errorMsg.html("号码不能为空");
        errorMsg.show();
        return false;
    } else if (/^[1][3,4,5,7,8][0-9]{9}$/.test(telNo) == false) {
        errorMsg.html("请输入正确的格式");
        errorMsg.show();
        return false;
    } else {
        errorMsg.hide();
        getTripper();
        console.log(travellerList);
        return true;
    }
}

//确认后提交订单
function chexkConfirm() {
    var name = $(".user-name").val();
    var idno = $(".user-idno").val();
    var telno = $(".user-telno").val();
    if (secKillId != null || secKillId != "") {
        var nowTime = new Date().getTime();
        var useTime = nowTime - rushTime;
        console.log("用时：" + useTime);
        if (useTime > (10 * 60 * 1000)) {
            layer.open({
                title: '抢购超时'
                , content: '请在十分钟内重新抢购'
            });
            return false;
        }
    }
    if ($(".checkid1").is(":checked") == false && $(".checkid2").is(":checked") == false) {
        console.log("未勾选");
        layer.open({
            title: '请阅读条款'
            , content: '在提交前请确认已阅读并同意以下条款'
        });
    } else if (name == "" || idno == "" || telno == "") {
        layer.open({
            title: '请阅读条款'
            , content: '请检查页面上所有选项是否正确填写'
        });
    } else {
        var orderName = proName;
        var childNumber = childNum;
        var personNumber = adultNum;
        var startDate = goDate;
        var orderMoney = $(".order-price").html();
        var productId = proId;
        var customerid = $(".customerid").val();

        getTripper();
        console.log(travellerList.toString());
        console.log("travellerList:" + travellerList + ",productId:" + productId);
        if (secKillId != null) {

            $.ajax({
                type: 'POST',
                url: '/secKill/success',
                datatype: 'json',
                traditional: true,
                data: {
                    "orderName": orderName,
                    "childNumber": childNumber,
                    "personNumber": personNumber,
                    "startDate": startDate,
                    "orderMoney": orderMoney,
                    "productId": productId,
                    "customeId": customerid,
                    "secKillId": secKillId,
                    "travellerList": JSON.stringify(travellerList),
                },
                success: function (data) {
                    console.log(data);
                    if (data.status == 200 && data.msg == "OK") {
                        //提交订单成功 进入购买界面
                        window.location.href = "/alipay/goPay?orderId=" + data.data;
                    } else {
                        alert(data.msg);
                    }
                }
            })
        } else {
            $.ajax({
                type: 'POST',
                url: '/order/add',
                datatype: 'json',
                traditional: true,
                data: {
                    "orderName": orderName,
                    "childNumber": childNumber,
                    "personNumber": personNumber,
                    "startDate": startDate,
                    "orderMoney": orderMoney,
                    "productId": productId,
                    "customeId": customerid,
                    "travellerList": JSON.stringify(travellerList),
                },

                success: function (data) {
                    console.log(data);
                    if (data.status == 200 && data.msg == "OK") {
                        //提交订单成功 进入购买界面
                        window.location.href = "/alipay/goPay?orderId=" + data.data;
                    } else {
                        alert(data.msg);
                    }
                }
            })
        }
    }
}

//点击名称进入首页
$(".nav_userName").click(function () {

    $.ajax({
        url: "/order/getId",
        type: "GET",
        datatype: "json",
        success: function (data) {
            console.log(data.data)
            window.sessionStorage.setItem("cusid", data.data)
            $(location).attr('href', 'order');
        }

    })

});
